<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="500px"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      size="mini"
      @keyup.enter.native="submitForm()"
      label-width="80px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="form.name" placeholder="活动名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="开始时间" v-model="form.startTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="结束时间" v-model="form.endTime"></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-check" type="primary" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>

import {getSeckill, saveSeckill, updateSeckill} from "@/api/coupon/seckill";

export default {
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  methods: {
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        getSeckill(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        });
      });
    },
    add() {
      this.form = {
        status: 1
      }
      this.visible = true
    },
    // 表单提交
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            saveSeckill(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('新增成功')
              }
            })
          } else {
            updateSeckill(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('更新成功')
              }
            })
          }
        }
      });
    }
  }
};
</script>
